package controller;

import exception.OtherException;
import exception.someException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @date 2025/9/26
 */
@Controller
@RequestMapping("/exception")
public class ExceptionController {
    //@ExceptionHandler标注在方法上，表示当前方法是一个用于局部异常处理的方法
    //该方法会处理当前类中所有的异常
    @ExceptionHandler(someException.class)
    public ModelAndView exceptionHandler(Exception e){
        ModelAndView mav = new ModelAndView();
        mav.addObject("msg",e);
        if(e instanceof someException){
            mav.setViewName("execption");
        }
        if(e instanceof OtherException){
            mav.setViewName("execption2");
        }
        return mav;
    }

    @RequestMapping("/f1")
    public void f1() throws someException{
        System.out.println("ExceptionController f1");
        throw new someException("执行f1方法遇到了异常");
    }

    @RequestMapping("/f2")
    public void f2() throws OtherException{
        System.out.println("ExceptionController f1");
        throw new OtherException("执行f2方法遇到了异常");
    }
}
